package org.ax.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.ax.data.Result;
import org.ax.pojo.sys.User;
import org.ax.pojo.sys.UserRole;
import org.ax.service.sys.UserRoleService;
import org.ax.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;

/**
 * 编程人:赵浩鑫
 * 时间:2021-03-12-17:32
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @GetMapping("/find")
    public ModelAndView find(){
        return new ModelAndView("sys/user");
    }
    @Autowired
    Result result;
    @Autowired
    UserService userService;
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,Integer limit){
        Page<User> userPage = userService.searchUserPage(new Page(page, limit));
        result.setData(userPage.getRecords());
        result.setCount(userPage.getTotal());
        return result;
    }
    @PostMapping("/add")
    public Result add(User user, String roleId, @RequestPart("file")MultipartFile file) throws IOException {
        UserRole userRole=new UserRole();
        userRole.setId(String.valueOf(System.currentTimeMillis()));
        userRole.setSysRoleId(roleId);
        userRole.setSysUserId(user.getId());
        if(!file.isEmpty()){
            String prefix=String.valueOf(System.currentTimeMillis());
//            String suffix=file.getOriginalFilename().split(".")[1];
//            String fileName=prefix+"."+suffix;
            System.out.println(file.getContentType());
            System.out.println(file.getName());
            System.out.println(file.getOriginalFilename());

            file.transferTo(new File("F:\\Y2data\\SSM\\SSM-production\\src\\main\\resources\\img\\"+file.getOriginalFilename()));
            user.setImg(file.getOriginalFilename());
        }
        result.setMsg(userService.addUser(user)&&userRoleService.addUserRole(userRole)?"添加用户成功":"添加用户失败");
        return result;
    }
    @PostMapping("/update")
    public Result update(User user,String roleId,@RequestPart("file")MultipartFile file) throws IOException {
        UserRole userRole=new UserRole();
        userRole.setSysRoleId(roleId);
        userRole.setSysUserId(user.getId());
        if(!file.isEmpty()){
            String prefix=String.valueOf(System.currentTimeMillis());
            String suffix=file.getOriginalFilename().substring(file.getOriginalFilename().indexOf('.'));
            String fileName=prefix+suffix;
            System.out.println(fileName);
            file.transferTo(new File("F:\\Y2data\\SSM\\SSM-production\\src\\main\\resources\\img\\"+fileName));
            user.setImg(fileName);
        }
        result.setMsg(userService.updateUser(user)&&userRoleService.updateUserRole(userRole)?"修改用户成功":"修改用户失败");
        return result;
    }
    @PostMapping("/updateStatus")
    public Result update(User user){
        result.setMsg(userService.updateUser(user)?"修改用户成功":"修改用户失败");
        return result;
    }
    @Autowired
    UserRoleService userRoleService;
    @PostMapping("/patchDel")
    public Result deletes(String[] ids){
        result.setMsg(userService.deletesBatch(ids)&&userRoleService.deletesUserRole(ids)?"删除用户成功":"删除用户失败");
        return result;
    }
}
